const Mock = require("mockjs");
const Random = Mock.Random;
let students = []
for (let i = 0; i < 10; i++) {

  let newStudentObject = {
    'key': i+1,
    "name": Random.cname(),//随机生成中文名字
    'id': /^\d{8,10}$/,
    'editable': false,
    'classNumber': /^\d{4,6}$/,
    "sex|1":["男","女"],
    'score': 1


  }
  students.push(newStudentObject)
}
students = Mock.mock(students)
//所有学生
const getStudents = () =>{
  return students;
}


//新增
const addStudent=(ops)=>{
  const student=JSON.parse(ops.body);
  students.push(student);
  return students;
}
//删除
const deleteStudent=(ops)=>{
  const value = JSON.parse(ops.body);
  students=students.filter(item=> item.key!==value);
  return students;
}
//修改学生
const changeStudent=(ops,ops1)=>{
  const student=JSON.parse(ops.body)
  const key=JSON.parse(ops1.body)
  students.forEach(item=>{
    if(item.key===key){
      item = student
    }
  })

  return students
}
//查询

module.exports = [
  {
    path: '/api/students/getStudents',
    type: 'get',
    func: getStudents
  },
  {
    path: '/api/students/addStudent',
    type: 'post',
    func: addStudent
  },
  {
    path: '/api/students/deleteStudent',
    type: 'post',
    func: deleteStudent
  },
  {
    path: '/api/students/changeStudent',
    type: 'post',
    func:changeStudent
  },

]